// pages/checkm/index.js
var utl = require('../../utils/util.js');
import drawQrcode from '../../utils/weapp.qrcode.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabarflag: true,
    inputText: '',
    bantk:'--',
    sk_url:'',  //静态码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryUserCard();
  },
  //查询用户卡
  queryUserCard: function () {
    utl.serverToken('/epay_apps/user/userInfoE0100', {}, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000
        })
        return;
      }
      var rsp = res.response;
      var bank = rsp.openBankName;
      var cardNm = rsp.accountNo;
      var len = cardNm.length;
      var rcode = cardNm.substring(len-3,len);
      var bak = bank+"(尾号"+rcode+")";
      this.setData({
        bantk:bak,
      })
    })
  },
  //去支付
  gopay: function () {
    var currnt = this.data.inputText;
    if (currnt == '') return;
    var money = parseFloat(currnt).toFixed(2);
    utl.serverToken('/epay_apps/payNew/payB0400', { 'transAmount': money, 'payId': '3' }, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000
        })
        return;
      }
      var link = res.response.payUrl;
      var i = link.indexOf("=");
      var orderid = link.substring(i + 1, link.length);
      var a = link.indexOf("?");
      var wurl = link.substring(0, a);
      wx.navigateTo({
        url: '/pages/checkm/webx?weburl=' + wurl + "&orderId=" + orderid,
      })
    })


    // wx.navigateTo({
    //   url: '/pages/payroad/index?money=' + currnt,
    // })
  },
  //输入金额
  jpinput: function (e) {
    console.log(e);
    var vale = e.currentTarget.dataset.info;
    var current = this.data.inputText;
    if (vale == '') return;
    if (current.length > 7) {
      wx.showToast({
        title: '输入金额超限',
        icon: 'none',
        duration: 1800
      })
      return;
    }
    if (vale == '0' && current == '' || vale == '00' && current == '') {
      return;
    }
    current += vale;
    this.setData({
      inputText: current
    })
  },
  //删除
  reduceAction: function () {
    var cvale = this.data.inputText;
    if (cvale == '') return;
    cvale = cvale.substring(0, cvale.length - 1);
    this.setData({
      inputText: cvale,
    })
  },
  tabbarAction: function () {
    this.setData({
      tabarflag: true
    })
  },
  tabsection: function () {
    var im = this.data.sk_url;
    if(im ==''){
      this.queryjtm();
    }
    this.setData({
      tabarflag: false
    })
  },
  //查询静态码
  queryjtm:function(){
    utl.serverToken('/epay_apps/scan/appScanH0100', {}, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000
        })
        return;
      }
      var img_url = res.response.url;
      this.draw(img_url);
    
    })
  },
  draw(e) {
    drawQrcode({
      width: 160,
      height: 160,
      x: 20,
      y: 20,
      canvasId: 'myQrcode',
      typeNumber: 10,
      text: e,
      image: {
        imageResource: '/pages/images/logicon.png',
        dx: 70,
        dy: 70,
        dWidth: 60,
        dHeight: 60
      },
      callback(e) {
        console.log('e: ', e)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  showImageAction: function () {
    // const irag = this.data.imgurl;
    // var arr = [irag];
    // wx.previewImage({
    //   current: '', // 当前显示图片的http链接
    //   urls: arr // 需要预览的图片http链接列表
    // })
  },
  saveAciton:function(){
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: 300,
      height: 300,
      destWidth: 300,
      destHeight: 300,
      canvasId: 'myQrcode',
      success(res) {
        console.log('图片的临时路径为：', res.tempFilePath)
        let tempFilePath = res.tempFilePath
        // 保存图片，获取地址
        // wx.saveFile({
        //   tempFilePath,
        //   success (res) {
        //     const savedFilePath = res.savedFilePath
        //     console.log('savedFilePath', savedFilePath)
        //   }
        // })

        // 保存到相册
        wx.saveImageToPhotosAlbum({
          filePath: tempFilePath,
          success: function (res) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: function (res) {
            wx.showToast({
              title: '保存失败',
              icon: 'none',
              duration: 2000
            })
          }
        })
      }
    })
  }
})